package com.lumm.controller.api;

import com.lumm.model.Message;
import com.lumm.model.RecommendItem;
import com.lumm.model.ResponseObject;
import com.lumm.model.User;
import com.lumm.services.DynamicService;
import com.lumm.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/api")
public class DynamicController {

    @Autowired
    private DynamicService dynamicService;

    @Autowired
    private UserService userService;

    @ResponseBody
    @RequestMapping(value = "sendDynamic", method = RequestMethod.GET)

    public ResponseObject sendDynamic(
            @RequestParam(value = "content") String content,
            HttpSession session
    ) {

        User user = (User) session.getAttribute("user");
        //用户未登录
        if (user == null || user.getUserId() == 0)
            return new ResponseObject(0, -1);
        return new ResponseObject(1, dynamicService.sendDynamic(user.getUserId(), content));

    }

    @ResponseBody
    @RequestMapping(value = "getFoundList", method = RequestMethod.GET)
    public ResponseObject getFoundList(
            HttpSession session
    ) {
        User user = (User) session.getAttribute("user");
        //用户未登录
        if (user == null || user.getUserId() == 0)
            return new ResponseObject(0, -1);

        return new ResponseObject(1, 1, dynamicService.getFoundList(user.getUserId(), 0, 20));
    }

    @ResponseBody
    @RequestMapping(value = "followUser", method = RequestMethod.GET)
    public ResponseObject followUser(
            @RequestParam(value = "userId", required = true) int userId,
            HttpSession session
    ) {
        User user = (User) session.getAttribute("user");
        //用户未登录
        if (user == null || user.getUserId() == 0)
            return new ResponseObject(0, -1);



        int status = dynamicService.followUser(user.getUserId(), userId);

        if (status == 0)
            return new ResponseObject(0, 0);
        return new ResponseObject(1, status);
    }

    @ResponseBody
    @RequestMapping(value = "getFollowingList", method = RequestMethod.GET)
    public ResponseObject getFollowingList(
            @RequestParam(value = "offset", required = false, defaultValue = "0") int offset,
            @RequestParam(value = "count", required = true) int count,
            HttpSession session
    ) {
        User user = (User) session.getAttribute("user");
        //用户未登录
        if (user == null || user.getUserId() == 0)
            return new ResponseObject(0, -1);

        List<RecommendItem> recommendItems=dynamicService.getFollowingList(user.getUserId(),offset,count);

        //int status = dynamicService.sendMessage(user.getUserId(),userId,content);

        if (recommendItems==null||recommendItems.size()==0)
            return new ResponseObject(0, 0);

        return new ResponseObject(1,1,recommendItems);
    }


    @ResponseBody
    @RequestMapping(value = "getFansList", method = RequestMethod.GET)
    public ResponseObject getFansList(
            @RequestParam(value = "offset", required = false, defaultValue = "0") int offset,
            @RequestParam(value = "count", required = true) int count,
            HttpSession session
    ) {
        User user = (User) session.getAttribute("user");
        //用户未登录
        if (user == null || user.getUserId() == 0)
            return new ResponseObject(0, -1);

        List<RecommendItem> recommendItems=dynamicService.getFansList(user.getUserId(),offset,count);

        //int status = dynamicService.sendMessage(user.getUserId(),userId,content);

        if (recommendItems==null||recommendItems.size()==0)
            return new ResponseObject(0, 0);

        return new ResponseObject(1,1,recommendItems);
    }


    @ResponseBody
    @RequestMapping(value = "sendMessage", method = RequestMethod.GET)
    public ResponseObject sendMessage(
            @RequestParam(value = "userId", required = true) int userId,
            @RequestParam(value = "content", required = true) String content,
            HttpSession session
    ) {
        User user = (User) session.getAttribute("user");
        //用户未登录
        if (user == null || user.getUserId() == 0)
            return new ResponseObject(0, -1);

        User otherUser = userService.querybyUserId(userId);
        //用户不存在
        if (otherUser == null || otherUser.getUserId() == 0)
            return new ResponseObject(0, 5);

        int status = dynamicService.sendMessage(user.getUserId(),userId,content);

        if (status == 0)
            return new ResponseObject(0, 0);
        return new ResponseObject(1, status);
    }

    @ResponseBody
    @RequestMapping(value = "getMessageList", method = RequestMethod.GET)
    public ResponseObject getMessageList(
            @RequestParam(value = "count", required = true) int count,
            HttpSession session
    ) {
        User user = (User) session.getAttribute("user");
        //用户未登录
        if (user == null || user.getUserId() == 0)
            return new ResponseObject(0, -1);

        List<Message> messages=dynamicService.getMessageList(user.getUserId(),0,count);

        //int status = dynamicService.sendMessage(user.getUserId(),userId,content);

        if (messages==null||messages.size()==0)
            return new ResponseObject(0, 0);

        return new ResponseObject(1,1,messages);
    }

    @ResponseBody
    @RequestMapping("deleteMessage")
    public ResponseObject deleteMessage(
            @RequestParam(value = "messageId", required = true) int messageId,
            HttpSession session
    ){
        User user = (User) session.getAttribute("user");
        //用户未登录
        if (user == null || user.getUserId() == 0)
            return new ResponseObject(0, -1);

        int status=dynamicService.deleteMessage(user.getUserId(),messageId);

        if (status!=1)
            return new ResponseObject(0,status);

        return new ResponseObject(1,1);

    }

}
